package com.huawei.hgip.paintgame.widget;

import com.huawei.hgip.paintgame.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.graphics.MaskFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class FingerPaintView extends View {
	
//	private static final float MINP = 0.25f;
//    private static final float MAXP = 0.75f;
    
    private int paint_color;
    
    private Bitmap  mBitmap;
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint mPaint;
    private Paint   mBitmapPaint;
    
    private MaskFilter  mEmboss;
    private MaskFilter  mBlur;
    
    public enum PaintStyles{NORMAL, EMBOSS, BLUR, ERASE, SRCATOP}

	public FingerPaintView(Context context, Bitmap bmap, Paint paint) {
		super(context);

        mPaint = paint;
		mBitmap = bmap;
		mBitmap.eraseColor(Color.BLACK);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
	}
	
	public FingerPaintView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.PaintView);
		paint_color = a.getColor(R.styleable.PaintView_paint_color, 0xFFFF0000);
		
		mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
		mBitmap.eraseColor(Color.BLACK);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
		mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
                0.4f, 6, 3.5f);
        mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
		
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setDither(true);
		mPaint.setColor(Color.WHITE);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mPaint.setStrokeWidth(3);
        
        //set def style
        setPaintStyle(PaintStyles.NORMAL);
		      
		a.recycle(); //回收资源
	}
	
	/**
	 * 设置画笔颜色
	 * @param color
	 */
	public void setPaintColor(int color){
		 mPaint.setColor(color);
	}
	
	public int getPaintColor() {
		return mPaint.getColor();
	}
	
	/**
	 * 设置画笔
	 * @param style
	 */
	public void setPaintStyle(PaintStyles style){
		switch (style) {
		case NORMAL:
			mPaint.setStyle(Paint.Style.STROKE);
	        mPaint.setStrokeJoin(Paint.Join.ROUND);
	        mPaint.setStrokeCap(Paint.Cap.ROUND);
	        mPaint.setStrokeWidth(12);
			break;
		case EMBOSS:
			if (mPaint.getMaskFilter() != mEmboss) {
				mPaint.setXfermode(null);
                mPaint.setMaskFilter(mEmboss);
            } else {
                mPaint.setMaskFilter(null);
            }
			break;
		case BLUR:
			if (mPaint.getMaskFilter() != mBlur) {
				mPaint.setXfermode(null);
                mPaint.setMaskFilter(mBlur);
            } else {
                mPaint.setMaskFilter(null);
            }
			break;
		case ERASE:
			mPaint.setMaskFilter(null);
			mPaint.setStyle(Paint.Style.STROKE);
			mPaint.setXfermode(new PorterDuffXfermode(
                    PorterDuff.Mode.CLEAR));
			break;
		case SRCATOP:
			mPaint.setXfermode(new PorterDuffXfermode(
                    PorterDuff.Mode.SRC_ATOP));
            mPaint.setAlpha(0x80);
			break;

		default:
			break;
		}
	}
	
	
	@Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFAAAAAA);
        
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        
        canvas.drawPath(mPath, mPaint);
    }
    
    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;
    
    protected void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }
    protected void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
            mX = x;
            mY = y;
        }
    }
    protected void touch_up() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath.reset();
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;
        }
        return true;
    }

}
